package online.lmds.sdmanage.main.base;

import online.lmds.sdmanage.common.Status;
import online.lmds.sdmanage.common.entity.BaseEntity;
import online.lmds.sdmanage.common.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Brighten Ma on 2017/4/30.
 */
public class BaseContoller {

    @Autowired
    public HttpServletRequest request;

    /**
     * 拦截Controller的异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception exception) {
        exception.printStackTrace();
        Result result = new Result();
        result.setErrorCode().setMessage("服务端异常" + " " + exception.getClass().getSimpleName()).setStatus(Status.SERVICER_ERROR);
        return result;
    }






    /**
     * 如果有权限检查，检查通过的话
     * 把request 中的 attribute 设置到 object 中
     */
    public void setUserInfo(BaseEntity object) {
        Integer userId = (Integer) request.getAttribute("userId");
        object.setUserId(userId);
    }
}
